package com.colliery.model;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.Map;

public class HttpUtil {
	 public static String httpRequest(String url, String body, Map<String, String> headers) {
	        try  {

	            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(SSLContext.getDefault(),
	                    NoopHostnameVerifier.INSTANCE);

	            Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
	                    .register("http", new PlainConnectionSocketFactory())
	                    .register("https", sslsf)
	                    .build();
	            PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
	            cm.setMaxTotal(100);
	            CloseableHttpClient httpClient = HttpClients.custom()
	                    .setSSLSocketFactory(sslsf)
	                    .setConnectionManager(cm)
	                    .build();


	            HttpPost request = new HttpPost(url);
	            StringEntity params = new StringEntity(body,"UTF-8");
	            for (Map.Entry<String, String> entry : headers.entrySet()) {
	                request.setHeader(entry.getKey(), entry.getValue());
	            }
	            request.setEntity(params);
	            HttpResponse result = httpClient.execute(request);
	            String ouput = EntityUtils.toString(result.getEntity(), "UTF-8");
	            return ouput;
	        } catch (IOException ex) {
	            ex.printStackTrace();
	        } catch (NoSuchAlgorithmException e) {
	            e.printStackTrace();
	        }
	        return "";
	    }
	}


